u`
EeLXgсFp.320uKv
EQ[JKF̕`AʑJځAʍĕ` Ȃ

p.320 K l

EKṔAp.308 lambda02.csƓȂ̂ŁAKQɐi݂܂傤B
EKQ́Ap.315 event02.csɂăAW悤
EfQ[g̐錾u߂lLALvɂ
ẺeŁACxgsʂreturnKv
E܂ACxgtB[hnullłꍇɂreturnKvB(͂𐳂݂̐̂Ƃ)
@-1ԂƂ悤
Eevent02Ɠd|łQ̐Ɏ󂯎Aa\IΗǂiJԂKv͂Ȃj
ECxgŌĂ΂NX쐬ȂĂǂ̂ŃVvɂł

p.320 KQ qgFEventClassNX

Eudelegate void Handler(char ch); //fQ[g̐錾(߂lȂAL)v
@udelegate int Handler(int x, int y); //fQ[g̐錾(߂lLAL)vƂ
EOnKeyHit\bh킹̂Łupublic int OnKeyHit(int x, int y) {vƂ
ECxg𔭐镔킹āureturn KeyHit(x, y);vƂ
EāACxgtB[h()łꍇA-1ԂƂ̂
@uelse { return -1; }v悤

p.320 KQ qgFShowNXMain\bh

EPɂ܂Ƃ߂悤
EL[1ڂ̒lێĂA2ڂ̃L[͂łQnăCxg𔭐Aʂ\悤ɂ悤

쐬

// p.320 KQ
using System;
delegate int Handler(int x, int y); //fQ[g̐錾(߂lLAL)
class EventClass { //Cxg̔SNX`
    public event Handler KeyHit; //CxgtB[hCxgKeyHitŒ`
    public int OnKeyHit(int x, int y) { //KeyHitCxg𔭐郁\bh
        if (KeyHit != null) //CxgtB[hnullłȂ
            return KeyHit(x, y); //Cxg𔭐ʂԂ
        else
            return -1; //CxgtB[hnullȂ-1Ԃ
    }
}
class lambda02 {
    public static void Main() {
        ConsoleKeyInfo cki; //L[̏Ԃ߂̃f[^\\
        EventClass ec = new EventClass(); //Cxg̔SNX
        ec.KeyHit += (x, y) => { return x + y; }; //Cxg̔ɂs\bhfQ[gɒǉ
        int left = -1; //Zp̍Ӓl
        while (true) { //[v
            if (Console.KeyAvailable) { //L[͂H
                cki = Console.ReadKey(false); //ꂽL[擾\
                char ch = cki.KeyChar; //ꂽL[̕擾
                if (Char.IsDigit(ch)) { //10i(0`9)L[H
                    int a = (int)char.GetNumericValue(ch); //ɕϊɃLXg
                    if (left == -1) { //Zp̍Ӓl܂Ȃ
                        left = a; //͒lӒlɂ
                        Console.WriteLine("ɉ̂́H"); //\
                    } else { //łȂΉEӒlȂ̂
                        int sum = ec.OnKeyHit(left, a); //Cxg𔭐Ęa𓾂
                        Console.WriteLine("Ȃ̂\n{0} + {1} = {2}", left, a, sum); //v\
                        break; //JԂ𔲂
                    }
                }
            }
        }
    }
}

13 O

p.321 O̊b

EvOɂĈُ펖ԂƁAʏA̎_ňُI
EvOɂẮAُ펖ԂO(Exception)ƂăvOɒʒmĂ
EāA󂯎ďdg݂荞łƂŁAُI
E̎dg݂O
EFdouble.Parse()\bhɁAϊłȂnƁA`OAΏȂƁA̎_ňُI
EΏgݍނɂ́AC#̏ꍇAtry-catch\ƁAExceptionNX̔hNXp

p.322 exception01.cs

//p.322 exception01.cs
using System;
class MyClass
{
    public static void Main()
    {
        Console.Write("鐔--");
        string strA = Console.ReadLine();
        double a = double.Parse(strA); //@FormatException̉\
        Console.Write("鐔---");
        string strB = Console.ReadLine();
        double b = double.Parse(strB); //AFormatException̉\
        Console.WriteLine("{0}  {1} = {2}", a, b, a / b);
    }
}

@FormatExceptioñbZ[Wɂ

EnhĂȂO: System.FormatException: ͕̌`܂B
@ ΏLqĂȂƂŁA`OƂ
Eꏊ System.Number.ParseDouble(String value, NumberStyles options, NumberFormatInfo numfmt)
@ꏊ System.Double.Parse(String s)
@ꏊ MyClass.Main() ꏊ C:\Users\human\Documents\ha231\chap13\Project1\exception01.cs:s 9
@ ǂނƁAmain9sڂŗOƂ킩B̏Qs͓IɌĂ΂ꂽ\bh
@ ŏʂɂ̂AĂ΂ꂽ\bhŎۂɗOꏊw
EĂяoX^bNƂAĂяȍɐςݏグC[W
Eq̂ƂAOƌĂяoɒʒm邱ƂJԂ

p.323iOFtry-catch\j

EO͂ǂȃ\bhłN肦̂ŁAΏׂOɍi荞ޕKv
EāA̗O͈͂AtryubNɂ
EtryubNɑāAcatchubNLqAȌuLq
EƁAcatchubN̍Ō̂PsIiKňُIɁAubŇ㑱sĊJ
EȂAtryubNɕs違OrŔꍇAtryubŇ㑱̏̓XLbvāAcatchubN1sڂɐi
E܂A̍\ł́AǂȗOĂΏłĂ܂iqΏ@j

p.324 exception02.cs

//p.324 exception02.cs
using System;
class MyClass {
    public static void Main() {
        double a = 0.0, b = 0.0;
        Console.Write("鐔--");
        string strA = Console.ReadLine();
        try { //OΏۇ@
            a = double.Parse(strA); //FormatException̉\
        } catch { //Ώۇ@̗Ȍ
            Console.WriteLine("sK؂ȓ͂ł"); 
        } //ُIɑs
        Console.Write("鐔---");
        string strB = Console.ReadLine();
        try { //OΏۇA
            b = double.Parse(strB); //FormatException̉\
        } catch { //ΏۇA̗Ȍ
            Console.WriteLine("sK؂ȓ͂ł");
        } //ُIɑs
        Console.WriteLine("{0}  {1} = {2}", a, b, a / b);
    }
}

AWKFp.324 exception02.cs

EϐaAbint^ɕύX悤
EƁA͒lɂāA`OɉăI[ot[Oƃ[ZO悤ɂȂ
E`OɉăI[ot[Ôǂ̗OĂusK؂ȓ͂łvƕ\Đɐił܂ƂmF悤
EȂA鐔̓͂ŗOƁA0ɂȂ̂ŁA[ZOُI

쐬

//AWKFp.324 exception02.cs
using System;
class MyClass {
    public static void Main() {
        int a = 0, b = 0;
        Console.Write("鐔--");
        string strA = Console.ReadLine();
        try { //OΏۇ@
            a = int.Parse(strA); //`O,I[ot[Ỏ\
        } catch { //Ώۇ@̗Ȍ
            Console.WriteLine("sK؂ȓ͂ł"); 
        } //ُIɑs
        Console.Write("鐔---");
        string strB = Console.ReadLine();
        try { //OΏۇA
            b = int.Parse(strB); //`O,I[ot[Ỏ\
        } catch { //ΏۇA̗Ȍ
            Console.WriteLine("sK؂ȓ͂ł");
        } //ُIɑs
        Console.WriteLine("{0}  {1} = {2}", a, b, a / b); //[ZO(ُI)̉\
    }
}

p.325iONXj

EFormatException̓NXłAExceptionNX̔hNX
EC#ł͗OExceptionNX̔hNXɂĕ\āÃIuWFNg𐶐ēn()dlɂȂĂ
EeONX̒ɂp֌WAc[ɍ\ꂢĂ
EFException̔hNXSystemExceptionŁA̔hNXFormatExceptionB
@܂AFormatException̔hNXiFileFormatExceptionȂǁj
EāAFormatExceptionpāA̔hNXɂOɂΏł

p.325iOFtry-catchO\j

EcatchɂėONXw肷邱ƂŁAOɈقȂΏ邱Ƃł
EF try {OΏ} catch(ONX ) {O}
EF try {OΏ} catch(ONX@ ) {O@} catch(ONXA ) {OA} c

p.326iONX̃oj

EeONX̊{NXłExceptionNXɂ́AOɗpłvpeB܂܂Ă
Estring MessagevpeBFObZ[W
Estring SourcevpeBFOvWFNg
EMethodBase TargetSitevpeBFO\bh(Console.Writeƃ\bhɂȂ)
Estring HelpLinkvpeBFwvt@CURL
Estring StackTracevpeBFO\bȟĂяo
E܂AObjectNXstring ToString()I[oChuOeԂv\bh񋟂Ă
 ObjectNXstring ToString()Console.WriteƏȗɎs

p.327 exception03.cs

//p.327 exception03.cs
using System;
class exception03 {
    public static void Main() {
        int[] arr = new int[5];
        try {
            arr[5] = 10; //IndexOutOfRangeException(Y͈͊O)
        } catch (IndexOutOfRangeException io) { //ɑ΂O
            Console.WriteLine(io); //Iio.ToString()Iɓ
            Console.WriteLine("[io]---------");
            Console.WriteLine(io.Source); //OvWFNg
            Console.WriteLine("[io.Source]---------");
            Console.WriteLine(io.Message); //ObZ[W
            Console.WriteLine("[io.Message]---------");
            Console.WriteLine(io.ToString()); //Oe
            Console.WriteLine("[io.ToString()]---------");
            Console.WriteLine(io.TargetSite); //O\bh
            Console.WriteLine("[io.TargetSite]---------");
        }
    }
}

p.326 System.ObjectNX

ESNẌÖق̊{NXƂĒ񋟂ĂAvO}NX`ĂAIɁASystem.ObjectNX̔hNXɂȂ
EIȂ̂ŁAp𖾎Kv͂Ȃ
EȂAC#̒ʏ̌^wł́uobjectv.NET^ł́uSystem.ObjectvƂȂ̂œ̂w
ẼNX̂ȃ\bhstring ToString()Łũ݂IuWFNg\Ԃvƒ`Ă
EC#񋟂NXł́A{IToString()I[oChāÃNX\ԂĂ
EvO}`NXɂĂALꍇ́AToString()I[oChāÃNX\`邱Ƃ
EȂAIuWFNg̎QƕϐConsole.WriteToString()s

p.329icatch̕Lqj

EcatchɂėONXw肷邱ƂŁAOɈقȂΏ邱Ƃł
EF 
@try {OΏ} 
@catch(ONX@ ) {O@} 
@catch(ONXA ) {OA} 
@F
EAOΏۂŗOƁALq̏ォ珇Ƀ}b`ÔŁAΉONX̊{NXɋLqĂƁu΂ɎsȂôŃG[vƂȂ̂Œ

eLXgю\Fp.329uexception04.csv
Q[JK\F̕`AʑJځAʍĕ` Ȃ

oFAWKFp.324 exception02.cs
